package day03;

import com.alibaba.druid.pool.DruidDataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbcp2.BasicDataSource;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;
import java.sql.Connection;

public class TestPool {

    private static String driverClassName="com.mysql.jdbc.Driver";
    private static String url="jdbc:mysql://127.0.0.1:3306/jdbc?useUnicode=true&characterEncoding=utf8";
    private static String username="root";
    private static String password="123";


    public static void main(String[] args) throws Exception {
//        DataSource dataSource=dbcp();
//        DataSource dataSource=c3p0();
        DataSource dataSource=druid();

        Connection con1=dataSource.getConnection();

        System.out.println(con1);

//        con1.close();

        Connection con2=dataSource.getConnection();

        System.out.println(con2);

        con2.close();

    }



    public static DataSource dbcp(){
        BasicDataSource dataSource=new BasicDataSource();
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);


        //最大连接数
        dataSource.setMaxTotal(1);
        //初始化连接数
        dataSource.setInitialSize(1);
        //最长等待时间
        dataSource.setMaxWaitMillis(5000);


        return dataSource;
    }



    public static DataSource c3p0() throws PropertyVetoException {

        ComboPooledDataSource dataSource=new ComboPooledDataSource();

        dataSource.setDriverClass(driverClassName);
        dataSource.setJdbcUrl(url);
        dataSource.setUser(username);
        dataSource.setPassword(password);


        dataSource.setMaxPoolSize(1);
        dataSource.setInitialPoolSize(1);
        dataSource.setCheckoutTimeout(5000);

        return dataSource;

    }

    public static DataSource druid(){
        DruidDataSource dataSource=new DruidDataSource();

        dataSource.setDriverClassName(driverClassName);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);


        dataSource.setMaxActive(1);
        dataSource.setInitialSize(1);
        dataSource.setMaxWait(5000);

        return dataSource;
    }
















































}
